package com.forestar.mapControl.mapview.multitouch;

import android.graphics.Color;
import android.util.DisplayMetrics;


import java.util.ArrayList;
import java.util.List;

public class SymbolUtils
  {
    public static List<String> svgFileName = new ArrayList<>();
    public static final int CALC_SIZE_TYPE_XDPI = 0;//
    public static final int CALC_SIZE_TYPE_AUTOMATIC = 1;  //屏幕适配后的动态计算出来的 dpi
    public static final int CALC_SIZE_TYPE_HANDLE = 2;//手动输入屏幕尺寸

    public final static int COMPLEX_UNIT_PX = 0;
    public final static int COMPLEX_UNIT_DIP = 1;
    public final static int COMPLEX_UNIT_SP = 2;
    public final static int COMPLEX_UNIT_PT = 3;
    public final static int COMPLEX_UNIT_IN = 4;
    public final static int COMPLEX_UNIT_MM = 5;
    public static DisplayMetrics screenDensity;
    public static boolean isFil = true;
//    private static SimpleTextSymbol simpleTextSymbol;
    private static float strokeBorderWidth;
    private static float labelDivideWidth;

    static
      {
        svgFileName.add("ic_anbiao");
        svgFileName.add("ic_beishi");
        svgFileName.add("ic_chuan");
        svgFileName.add("ic_dengta");
        svgFileName.add("ic_dengzhuang");
        svgFileName.add("ic_dizhantai");
        svgFileName.add("ic_dushanshi");
        svgFileName.add("ic_fanghuozhan");
        svgFileName.add("ic_fangwu");
        svgFileName.add("ic_fubiao");
        svgFileName.add("ic_huosankou");
        svgFileName.add("ic_jianchazhan");
        svgFileName.add("ic_jiantou");
        svgFileName.add("ic_jiayouzhan");
        svgFileName.add("ic_kengxue");
        svgFileName.add("ic_langchang");
        svgFileName.add("ic_linyezhan");
        svgFileName.add("ic_miaotang");
        svgFileName.add("ic_miaoyu");
        svgFileName.add("ic_qingzhansi");
        svgFileName.add("ic_qizi");
        svgFileName.add("ic_shizi");
        svgFileName.add("ic_shengtaijiance");
        svgFileName.add("ic_shu");
        svgFileName.add("ic_shuangyuan");
        svgFileName.add("ic_shuita");
        svgFileName.add("ic_tianwentai");
        svgFileName.add("ic_tingchechang");
        svgFileName.add("ic_tongxinta");
        svgFileName.add("ic_weixing");
        svgFileName.add("ic_wuxing");
        svgFileName.add("ic_xinhaogan");
        svgFileName.add("ic_xuexiao");
        svgFileName.add("ic_yanjing");
      }

    private static int calcSizeType = CALC_SIZE_TYPE_AUTOMATIC;

    public static void setCalcSizeType(int calcSizeType)
      {
        SymbolUtils.calcSizeType = calcSizeType;
      }

    public static DisplayMetrics getScreenDensity()
      {
        return screenDensity;
      }

    public static void setScreenDensity(DisplayMetrics screenDensity)
      {
        SymbolUtils.screenDensity = screenDensity;
      }

//    public static ISymbol createDefaultSymbol(IFeatureClass featureClass)
//      {
//        return createDefaultSymbolByGeometryType(featureClass.getGeometryType());
//      }

//    public static ISymbol createDefaultSymbolByGeometryType(GeometryType geometryType)
//      {
//        switch (geometryType)
//          {
//            case GeometryTypeUnknown:
//              return null;
//            case GeometryTypePoint:
//            case GeometryTypeMultiPoint:
//              return createDefaultPointSymbol();
//            case GeometryTypePolyline:
//            case GeometryTypeMultiPolyline:
//              return createDefaultLineSymbol();
//            case GeometryTypePolygon:
//            case GeometryTypeMultiPolygon:
//              return createDefaultPolygonSymbol();
//            default:
//              return null;
//          }
//      }
//
//    public static ISymbol createSelectSymbol(IFeatureClass featureClass)
//      {
//        switch (featureClass.getGeometryType())
//          {
//            case GeometryTypeUnknown:
//              return null;
//            case GeometryTypePoint:
//            case GeometryTypeMultiPoint:
//              return createSelectPointSymbol();
//            case GeometryTypePolyline:
//            case GeometryTypeMultiPolyline:
//              return createSelectLineSymbol();
//            case GeometryTypePolygon:
//            case GeometryTypeMultiPolygon:
//              return createSelectPolygonSymbol();
//            default:
//              return null;
//          }
//      }
//
//    public static ISymbol createMergeSymbol(IFeatureClass featureClass)
//      {
//        switch (featureClass.getGeometryType())
//          {
//            case GeometryTypeUnknown:
//              return null;
//            case GeometryTypePoint:
//            case GeometryTypeMultiPoint:
//              return null;
//            case GeometryTypePolyline:
//            case GeometryTypeMultiPolyline:
//              return createMergeLineSymbol();
//            case GeometryTypePolygon:
//            case GeometryTypeMultiPolygon:
//              return createMergePolygonSymbol();
//            default:
//              return null;
//          }
//      }
//
//    public static ISymbol createSelectPointSymbol()
//      {
//        SimplePointSymbol sp = new SimplePointSymbol(SimplePointSymbol.PointStyleCircle, 1, 0.3f, Color.BLUE, true, Color.YELLOW);
//        sp.setFocus(true);
//        return sp;
//      }
//
//    public static ISymbol createMergePointSymbol()
//      {
//        SimplePointSymbol sp = new SimplePointSymbol(SimplePointSymbol.PointStyleCircle, 3.4f, 0.3f, Color.BLUE, true, Color.RED);
//        sp.setFocus(true);
//        return sp;
//      }
//
//    public static ISymbol createSelectPolygonSymbol()
//      {
//        SimplePolygonSymbol simplePolygonSymbol = new SimplePolygonSymbol(0.5F, Color.rgb(0, 255, 255), false, Color.YELLOW);
//        simplePolygonSymbol.setAlpha(100);
//        return simplePolygonSymbol;
//      }
//
//    public static ISymbol createSelectLineSymbol()
//      {
//        return new SimplePolylineSymbol(0.8f, Color.YELLOW, true, 0.3f, Color.rgb(0, 255, 255));
//      }
//
//    public static SimplePolygonSymbol createMergePolygonSymbol()
//      {
//        SimplePolygonSymbol simplePolygonSymbol = new SimplePolygonSymbol(1F, Color.RED, true, Color.rgb(238, 201, 0));
//        simplePolygonSymbol.setAlpha(100);
//        return simplePolygonSymbol;
//      }
//
//    public static ISymbol createMergeLineSymbol()
//      {
//        return new SimplePolylineSymbol(0.3f, Color.rgb(255, 165, 0), true, 0.3f, Color.rgb(255, 165, 0));
//      }
//
//    public static ISymbol createDefaultPointSymbol()
//      {
//        return new SimplePointSymbol(SimplePointSymbol.PointStyleCircle, 1.8f, 0.3f, Color.WHITE, true, Color.GREEN);
//      }
//
//    public static SimplePointSymbol createVertexSymbol()
//      {
//        return new SimplePointSymbol(SimplePointSymbol.PointStyleCircle, 1.8f, 0.3f, Color.RED, true, Color.WHITE, 255);
//      }
//
//    public static SimplePointSymbol createVertexHeadSymbol()
//      {
//        return new SimplePointSymbol(SimplePointSymbol.PointStyleCircle, 1.8f, 0.3f, Color.RED, false, Color.GREEN, 255);
//      }
//
//    public static SimplePointSymbol createVertexCatchPotionSymbol()
//      {
//        return new CatchPointSymbol(SimplePointSymbol.PointStyleCircle, 1.8f, 0.3f, Color.RED, false, Color.GREEN);
//      }
//
//    public static SimplePointSymbol createVertexTailSymbol()
//      {
//        return new SimplePointSymbol(SimplePointSymbol.PointStyleCircle, 1.8f, 0.3f, Color.RED, true, Color.BLUE, 255);
//      }
//
//    public static SimplePointSymbol createVertexMidPointSymbol()
//      {
//        return new SimplePointSymbol(SimplePointSymbol.PointStyleCircle, 1, 0.6f, Color.RED, true, Color.WHITE);
//      }
//
//    public static ISymbol createDefaultPolygonSymbol()
//      {
//        return new SimplePolygonSymbol(0.3f, Color.YELLOW, true, Color.RED);
//      }
//
//    public static SketchSymbol createSketchSymbol()
//      {
//        return new SketchSymbol(6, Color.RED, true, Color.argb(96, 255, 255, 0));
//      }
//
//    public static ISymbol createDefaultLineSymbol()
//      {
//        return new SimplePolylineSymbol(0.3f, Color.argb(255, 255, 0, 255), true, 1, Color.argb(255, 255, 255, 255));
//      }
//
//    public static SimpleTextSymbol createDefaultSimpleTextSymbol()
//      {
//        if (simpleTextSymbol != null)
//          {
//            return simpleTextSymbol;
//          }
//        simpleTextSymbol = new SimpleTextSymbol();
//        return simpleTextSymbol;
//      }
//
//    public static ISymbol createSymbol(GeometryType gt, int borderColor, int fillColor, boolean isFill, float borderWidth, float size)
//      {
//        ISymbol iSymbol = null;
//        switch (gt)
//          {
//            case GeometryTypePoint:
//            case GeometryTypeMultiPoint:
//              iSymbol = new SimplePointSymbol(SimplePointSymbol.PointStyleCircle, size, borderWidth, borderColor, isFill, fillColor);
//              break;
//            case GeometryTypePolyline:
//            case GeometryTypeMultiPolyline:
//              iSymbol = new SimplePolylineSymbol(size, fillColor, isFill, borderWidth, borderColor);
//              break;
//            case GeometryTypePolygon:
//            case GeometryTypeMultiPolygon:
//              iSymbol = new SimplePolygonSymbol(borderWidth, borderColor, isFill, fillColor);
//              break;
//          }
//        return iSymbol;
//      }

    /**
     * 面填充设置 里面没有刷新方法
     * <p>
     * TODO
     * </p>
     *
     * @param isFill
     */
//    public static void changeFillStatus(GeoMap geoMap, boolean isFill)
    public static void changeFillStatus(boolean isFill)
      {
//        if (geoMap == null)
//          {
//            return;
//          }
        //        List<ILayer> featureLayers = geoMap.getDataLayers();
        //        ILayer layer;
        //        FeatureLayer featureLayer;
        //        int dataLayerCount = featureLayers.size();
        //        int alpha = isFill ? 255 : 0;
        //        for (int i = 0; i < dataLayerCount; i++)
        //          {
        //            layer = featureLayers.get(i);
        //            if (!(layer instanceof FeatureLayer))
        //              {
        //                continue;
        //              }
        //            featureLayer = (FeatureLayer) layer;
        //            if (featureLayer.getGeometryType() != GeometryType.GeometryTypePolygon)
        //              {
        //                continue;
        //              }
        //            featureLayer.setFeatureAlpha(alpha);
        //            FeatureRenderer featureRenderer = featureLayer.getFeatureRenderer();
        //            if (featureRenderer instanceof UniqueFeatureRenderer)
        //              {
        //                UniqueFeatureRenderer uniFeatureRenderer = (UniqueFeatureRenderer) featureRenderer;
        //                //修改Default的面填充
        //                SimplePolygonSymbol uniDefaultSymbol = (SimplePolygonSymbol) uniFeatureRenderer.getDefaultRendererItem().getSymbol();
        //                uniDefaultSymbol.setAlpha(alpha);
        //                //有条件的FeatureRendererItem的面填充
        //                List<FeatureRendererItem> uniRenderItems = uniFeatureRenderer.getFeatureRendererItems();
        //                for (int j = 0; j < uniRenderItems.size(); j++)
        //                  {
        //                    uniRenderItems.get(j).getSymbol().setAlpha(alpha);
        //                  }
        //              } else if (featureRenderer instanceof SimpleFeatureRenderer)
        //              {
        //                SimpleFeatureRenderer simpleFeatureRenderer = (SimpleFeatureRenderer) featureRenderer;
        //                SimplePolygonSymbol symbol = (SimplePolygonSymbol) simpleFeatureRenderer.getSymbol();
        //                symbol.setAlpha(alpha);
        //                simpleFeatureRenderer.setSymbol(symbol);
        //              } else if (featureRenderer instanceof UniqueValueFeatureRender)
        //              {
        //                UniqueValueFeatureRender uniqueValueFeatureRender = (UniqueValueFeatureRender) featureRenderer;
        //                //修改Default的面填充
        //                SimplePolygonSymbol uniDefaultSymbol = (SimplePolygonSymbol) uniqueValueFeatureRender.getDefaultRendererItem().getSymbol();
        //                uniDefaultSymbol.setAlpha(alpha);
        //                //有条件的FeatureRendererItem的面填充
        //                List<FeatureRendererItem> uniRenderItems = uniqueValueFeatureRender.getFeatureRendererItems();
        //                for (int j = 0; j < uniRenderItems.size(); j++)
        //                  {
        //                    uniRenderItems.get(j).getSymbol().setAlpha(alpha);
        //                  }
        //              }
        //          }
        isFil = isFill;
      }

    public static float getLabelDivideWidth()
      {
        if (labelDivideWidth > 0)
          {
            return labelDivideWidth;
          }
        labelDivideWidth = applyDimension(COMPLEX_UNIT_MM, 0.2f);
        return labelDivideWidth;
      }

    public static float getStrokeBorderWidth()
      {
        if (strokeBorderWidth > 0)
          {
            return strokeBorderWidth;
          }
        strokeBorderWidth = applyDimension(COMPLEX_UNIT_MM, 0.2f);
        return strokeBorderWidth;
      }

    /**
     * 是否一键透明
     *
     * @deprecated 由 {@link }统一管理  com.mz_utilsas.forestar.utils.MapzoneConfig
     */
    @Deprecated
    public static boolean isMapTransparent()
      {
        return !isFil;
      }

    public static float applyDimension(int unit, float value)
      {
        float dpi = getCalcUseDpi();
        switch (unit)
          {
            case COMPLEX_UNIT_PX:
              return value;
            case COMPLEX_UNIT_DIP:
              return value * screenDensity.density;
            case COMPLEX_UNIT_SP:
              return value * screenDensity.scaledDensity;
            case COMPLEX_UNIT_PT:
              return value * dpi * (1.0f / 72);
            case COMPLEX_UNIT_IN:
              return value * dpi;
            case COMPLEX_UNIT_MM:
              return value * dpi * (1.0f / 25.4f);

          }
        return 0;
      }

    private static float getCalcUseDpi()
      {
        float calcUseDpi = 0;
        switch (calcSizeType)
          {
            case CALC_SIZE_TYPE_XDPI:
              calcUseDpi = screenDensity.xdpi;
              break;
            case CALC_SIZE_TYPE_AUTOMATIC:
              calcUseDpi = screenDensity.densityDpi;
              break;
            case CALC_SIZE_TYPE_HANDLE:
//              calcUseDpi = LayoutAdaption.getRealDensityDpi();
              break;
            default:
              calcUseDpi = screenDensity.xdpi;
              break;
          }
        return calcUseDpi;
      }

    /**
     * 转换成像素
     *
     * @param unit
     * @param value
     * @return
     */
    public static int complexToDimensionPixelSize(int unit, float value)
      {
        final float f = applyDimension(unit, value);
        final int res = (int) (f + 0.5f);
        if (res != 0)
          return res;
        if (value == 0)
          return 0;
        if (value > 0)
          return 1;
        return -1;
      }

    private static int getColorRandom()
      {
        return Color.rgb((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
      }

//    public static ISymbol createRandomSymbol(IFeatureClass featureClass)
//      {
//        switch (featureClass.getGeometryType())
//          {
//            case GeometryTypeUnknown:
//              return null;
//            case GeometryTypePoint:
//            case GeometryTypeMultiPoint:
//              return createRandomPointSymbol();
//            case GeometryTypePolyline:
//            case GeometryTypeMultiPolyline:
//              return createRandomPolylineSymbol();
//            case GeometryTypePolygon:
//            case GeometryTypeMultiPolygon:
//              return createRandomPolygonSymbol();
//            default:
//              return null;
//          }
//      }
//
//    public static ISymbol createRandomPointSymbol()
//      {
//        return new SimplePointSymbol(SimplePointSymbol.PointStyleCircle, 3, 0.3f, getColorRandom(), true, getColorRandom());
//      }
//
//    public static ISymbol createRandomPolylineSymbol()
//      {
//        return new SimplePolylineSymbol(0.3f, getColorRandom(), true, 0.3f, getColorRandom());
//      }
//
//    public static ISymbol createRandomPolygonSymbol()
//      {
//        return new SimplePolygonSymbol(0.3f, getColorRandom(), true, getColorRandom());
//      }
//
//    public static SimplePolylineSymbol createSketchOverlayLayerSymbol()
//      {
//        return new SimplePolylineSymbol(0.5f, Color.argb(255, 0, 0, 255), true, 1, Color.argb(255, 255, 255, 255));
//      }
  }